<?php
namespace app\index\controller;
use app\common\redis\RedisPackage;
use think\Controller;
use think\Db;

/**
 * 系统管理类
 * @author Administrator
 * 路由管理 信息管理 
 */
class Systemmanagement extends Common
{
    
    // 信息公告查询
	public function xxgg_cx()
	{
	    /* ->whereTime("youxiao_shijian",">=",time()) */
		$result = db('xinxi')->field("id,biaoti,shijian")->select();
//		foreach ($result as $k => $v){
//		    $result[$k]['shijian'] = date("Y-m-d H:i:s",$v['shijian']);
//		}
		returnSuccess($result);
	}

	// 信息公告基础信息
	public function xxgg_fsdx()
	{
		$result = db('zaixian_lb')->select();
		returnSuccess($result);
	}

	// 添加信息公告
	public function xxgg_add()
	{	
	    $data['date_start'] = input('date_start','','trim');
	    $data['name'] = input('name','','trim');
	    $data['content'] = input('content','','trim');
	    $data['yh_lb'] = input('yh_lb/d');
	    if (!$data['date_start'] || $data['date_start'] == "NaN-NaN-NaN") returnError("请确认有效时间是否填写正确");
	    if (!$data['content']) returnError("内容不能为空");
	    if (!$data['name']) returnError("标题不能为空");
	    if (!$data['yh_lb']) returnError("请选择发送对象");
	    $time = strtotime($data['date_start']);
	    $shuju = [
	        'biaoti'           => $data['name'],
	        'beizhu'           => $data['content'],
	        'yh_lb'            => $data['yh_lb'],
	        'youxiao_shijian'  => $time,
	        'shijian'          => time(),
	        'gt_jujian_id'     => $this->gt_jujian_id,
	        'lb'               => 3,
	    ];
		$result = db('xinxi')->insertGetId($shuju);
        if($result>0){
            $id= $this->id;
            if($this->isCZY){
                $id = $this->jujian['par_id'];
            }
            RedisPackage::getInstance();
            RedisPackage::RPush('send_sms',"6;{$id};{$data['yh_lb']};0;{$result};");

            $operation_category = 32;
            $detail_name = array('发送消息','消息ID','消息标题');
            $operation_value = array('成功',$result,$data['name']);
            $re_name = ['tjxx_add'];
            $re_value = ['1'];
            $arr = array_combine($detail_name, $operation_value);
            $redis = array_combine($re_name, $re_value);
            $this->riZhi($operation_category, $arr, $redis);
            returnSuccess([],"操作成功");
        }else {
            returnError("添加信息公告失败");
        }
	}

	// 删除信息公告
	public function xxgg_scxx()
	{
		$data = input();
		if (!isset($data['xz']) || $data['xz'] < 0) returnError("参数错误");
		$res = db('xinxi')->where('id',$data['xz'])->find();
		if($res){
		    db('xinxi')->where('id',$data['xz'])->delete();
            $operation_category = 54;
            $detail_name = array('删除消息公告','消息ID','消息标题');
            $operation_value = array('成功',$data['xz'],$res['biaoti']);
            $re_name = ['news_del'];
            $re_value = ['1'];
            $arr = array_combine($detail_name, $operation_value);
            $redis = array_combine($re_name, $re_value);
            $this->riZhi($operation_category, $arr, $redis);
            returnSuccess([],"操作成功");
        }else {
            returnError("操作失败");
        }
	}

    //实盘帐户
	public function spzh_jzmb()
	{
		$result = db('trade_service_info')->where('isuse',1)->where('ib_id',$this->gt_jujian_id)->field("id,investor_id")->select();
		returnSuccess($result);
	}
	
	//加载数据
	public function spzh_jzsj()
	{
	    $zhanghao_id = input('id','0','intval');
	    if ($zhanghao_id == 0) returnError("参数错误");
	    $kongzhibiao_data = array();
	    $heyue_new = db('heyue')->where('xs_bz',1)->field('id,bh,name')->select();
		$result_zhanghao_kongzhi=db('zhanghao_kongzhi')->where('zhanghao_id',$zhanghao_id)->select();
		if(!empty($result_zhanghao_kongzhi)){
			//两联表查询
			$kongzhibiao_data = db('heyue')
            ->alias('h')
            ->join('zhanghao_kongzhi z','h.id = z.heyue_id')
			->where('h.xs_bz',1)
			->where('z.zhanghao_id',$zhanghao_id)
			->field('h.id,h.bh,h.name,z.zong_num')
            ->select();
			//遍历出来表中的heyue_id
			$he_number = array();
			foreach($kongzhibiao_data as $x=>$val)
            {
                $he_number[$x] = $val['id'];
            }
			//查出不存在zhanghao_kongzhi表中的数据
			$heyue_new = db('heyue')
			->where('xs_bz',1)
			->where('id','not in',$he_number)
			->field('id,bh,name')
			->select();
		}
		foreach($heyue_new as $x=>$val)
        {
            $heyue_new[$x]['zong_num'] = 0;
        }
		$result_heyue = array_merge($kongzhibiao_data,$heyue_new); 	
		returnSuccess($result_heyue);
	}
	
	// 修改手数
	public function spzh_xgss()
	{
		$data = input();
		if (!isset($data['zong_num'])) returnError("缺少重要参数");
		$shuju = $data;
		unset($shuju['token']);
		$dat = db('zhanghao_kongzhi')
		->where('zhanghao_id',$data['zhanghao_id'])
		->where('heyue_id',$data['heyue_id'])
		->select();
		if(!empty($dat)){
			$res = db('zhanghao_kongzhi')
		     ->where('zhanghao_id',$data['zhanghao_id'])
		     ->where('heyue_id',$data['heyue_id'])
			 ->update($shuju);
		}else{
			$res = db('zhanghao_kongzhi')->insertGetId($shuju);
		}
		if($res>0){
		    returnSuccess([],'操作成功');
		}else{
		    returnError("操作失败");
	    }
	}
	
	// 添加账户
	public function spzh_tjzh()
	{
		$data['ib_id'] = $this->gt_jujian_id;
		$data['appid'] = input('appid',"",'trim');
		$data['lastinfo'] = input('lastinfo',"","trim");
		$data['investor_passwd'] = input("investor_passwd","","trim");
		$data['front_addr'] = input("front_addr","","trim");
		$data['front_port'] = input("front_port","","trim");
		$data['certinfo'] = input("certinfo","","trim");
		$data['investor_id'] = input("investor_id",'',"trim");
		$data['broker_id'] = input("broker_id","","trim");
		$data['client_id'] = input("client_id/d");
		$data['isuse'] = input("isuse/d");
		$data['nwp_bz'] = input("nwp_bz/d");
		$data['trade_type'] = input("trade_type/d");
		if (!$data['appid']) returnError("AppID不能为空");
		if (!$data['broker_id'] && $data['broker_id'] != 0 ) returnError("BrokerID不能为空");
		if (!$data['certinfo']) returnError("授权码不能为空");
		if (!$data['front_addr']) returnError("交易地址不能为空");
		if (!$data['front_port']) returnError("交易接口不能为空");
		if (!$data['investor_id']) returnError("账户名不能为空");
		if (!$data['investor_passwd']) returnError("密码不能为空");
		if (!$data['isuse'] && $data['isuse'] != 0) returnError("请选择是否启用");
		if (!$data['client_id'] && $data['client_id'] != 0) returnError("客户端ID不能为空");
	    if (!$data['nwp_bz']) returnError("请先选择内外盘标志");
	    if (!$data['trade_type']) returnError("请先选择账户类型");
		$result = db('trade_service_info')->insertGetId($data);
        if($result>0){
            $operation_category = 33;
            $detail_name = array('添加实盘账户','实盘账户ID','账户名','BrokerID','交易地址','交易端口','客户端ID');
            $operation_value = array('成功',$result,$data['investor_id'],$data['broker_id'],$data['front_addr'],$data['front_port'],$data['client_id']);
            $re_name = ['spzh_add'];
            $re_value = ['1'];
            $arr = array_combine($detail_name, $operation_value);
            $redis = array_combine($re_name, $re_value);
            $this->riZhi($operation_category, $arr, $redis);
            returnSuccess([],"操作成功");
        }else{
            returnError("操作失败");
        }
	}
	
	// 编辑账户
	public function spzh_bjzh(){
		$data['ib_id'] = $this->gt_jujian_id;
		$data['appid'] = input('appid',"",'trim');
		$data['lastinfo'] = input('lastinfo',"","trim");
		$data['investor_passwd'] = input("investor_passwd","","trim");
		$data['front_addr'] = input("front_addr","","trim");
		$data['front_port'] = input("front_port","","trim");
		$data['certinfo'] = input("certinfo","","trim");
		$data['investor_id'] = input("investor_id",'',"trim");
		$data['broker_id'] = input("broker_id","","trim");
		$data['id'] = input("id/d");
		$data['client_id'] = input("client_id/d");
		$data['isuse'] = input("isuse/d");
		$data['nwp_bz'] = input("nwp_bz/d");
		$data['trade_type'] = input("trade_type/d");
		if (!$data['id']) returnError("缺少重要参数全局ID");
		if (!$data['appid']) returnError("AppID不能为空");
		if (!$data['broker_id'] && $data['broker_id'] != 0 ) returnError("BrokerID不能为空");
		if (!$data['certinfo']) returnError("授权码不能为空");
		if (!$data['front_addr']) returnError("交易地址不能为空");
		if (!$data['front_port']) returnError("交易接口不能为空");
		if (!$data['investor_id']) returnError("账户名不能为空");
		if (!$data['investor_passwd']) returnError("密码不能为空");
		if (!$data['isuse'] && $data['isuse'] != 0) returnError("请选择是否启用");
		if (!$data['client_id'] && $data['client_id'] != 0) returnError("客户端ID不能为空");
		if (!$data['nwp_bz']) returnError("请先选择内外盘标志");
		if (!$data['trade_type']) returnError("请先选择账户类型");
		$result = db('trade_service_info')->update($data);
        if($result>0){
            $operation_category = 34;
            $detail_name = array('添加实盘账户','实盘账户ID','账户名','BrokerID','交易地址','交易端口','客户端ID');
            $operation_value = array('成功',$data['id'],$data['investor_id'],$data['broker_id'],$data['front_addr'],$data['front_port'],$data['client_id']);
            $re_name = ['spzh_edit'];
            $re_value = ['1'];
            $arr = array_combine($detail_name, $operation_value);
            $redis = array_combine($re_name, $re_value);
            $this->riZhi($operation_category, $arr, $redis);
            returnSuccess([],"操作成功");
        }else{
            returnError("操作失败");
        }
	}
	
	// 编辑账户前请求数据
	public function bjzh_shuju(){
		$data = input();
		$result_zhlx = db('trade_lb')->select();
		$result_spzh = db('trade_service_info')->where('id',$data['id'])->select();
		$result = array($result_zhlx,$result_spzh);
		returnSuccess($result);
	}
	
    // 账户类型
	public function zhlx()
    {
		$result = db('trade_lb')->select();
		returnSuccess($result);
	}
	
	// 加载路由模板
	public function jzlymb()
	{
		$result = db('luyou_zu')
            ->where('ib_id',$this->gt_jujian_id)
            ->where('bz',1)
            ->field("id,name")
            ->select();
		returnSuccess($result);
	}
	
	//删除路由组
	public function lymb_scmb()
    {
        $data = input();
        if (!isset($data['id'])) returnError("缺少参数");
        $judge = db('yonghu')
            ->where('luyou_zu_id',$data['id'])
            ->select();
        $res = array();
        if(count($judge)>0){
            returnError("此模板正在使用中，无法删除");
        }else{
            $result = db('luyou_zu')
                ->where('id',$data['id'])
                ->update(['bz'=>0]);
            if($result>0){
                returnSuccess([],"操作成功！");
            }else{
                returnError("删除路由模板失败!");
            }
        }
    }
    
    // 加载数据
	public function lymb_jzsj()
	{
		$data = input();
		if (!isset($data['id'])) returnError("缺少参数");
		$result = db('luyou_zu_detail')->where('zu_id',$data['id'])->select();
		returnSuccess($result);
	}
	
	// 添加路由组
	public function lymb_tjmb()
	{
		$data['name'] = input('name','','trim');
		$data['beizhu'] = input('beizhu','','trim');
		if (!$data['name']) returnError("模板名称不能为空");
		if (!$data['beizhu']) returnError("请填写说明");
        $data['bz'] = 1;
        $data['ib_id'] = $this->gt_jujian_id;
		$result = db('luyou_zu')->insertGetId($data);
        if($result>0){
            $operation_category = 35;
            $detail_name = array('添加路由组','路由组ID','路由组名称','路由组说明');
            $operation_value = array('成功',$result,$data['name'],$data['beizhu']);
            $re_name = ['lyz_add'];
            $re_value = ['1'];
            $arr = array_combine($detail_name, $operation_value);
            $redis = array_combine($re_name, $re_value);
            $this->riZhi($operation_category, $arr, $redis);
            returnSuccess([],"操作成功");
        }else{
            returnError("操作失败");
        }
	}
	
	// 确认修改模板
	public function lymb_xgmb()
	{	
	    $data['id'] = input('id/d');
	    $data['name'] = input('name','','trim');
	    $data['beizhu'] = input('beizhu','','trim');
	    if (!$data['name']) returnError("模板名称不能为空");
	    if (!$data['beizhu']) returnError("请填写说明");
	    if (!$data['id']) returnError("缺少参数");
		$result= db('luyou_zu')->where('id',$data['id'])->update(['name'=>$data['name'],'beizhu'=>$data['beizhu']]);
        if($result>0){
            $operation_category = 36;
            $detail_name = array('编辑路由组','路由组ID','路由组名称','路由组说明');
            $operation_value = array('成功',$data['id'],$data['name'],$data['beizhu']);
            $re_name = ['lyz_edit'];
            $re_value = ['1'];
            $arr = array_combine($detail_name, $operation_value);
            $redis = array_combine($re_name, $re_value);
            $this->riZhi($operation_category, $arr, $redis);
            returnSuccess([],"操作成功");
        }else{
            returnError("操作失败");
        }
	}
	
	// 修改路由组
	public function xglyz()
	{	
	    $data['id'] = input('id/d');
	    if (!$data['id']) returnError("缺少参数");
		$result = db('luyou_zu')->where('id',$data['id'])->find();
		returnSuccess($result);
	}

	// 添加数据
	public function table_tjsj()
	{
	    $data['nw_bz'] = input('nw_bz/d');
	    $data['zu_id'] = input('zu_id/d');
	    $data['zhanghao_id'] = input('zhanghao_id/d');
	    $data['heyue_zu_bh'] = input('heyue_zu_bh','','trim');
	    if (!$data['zhanghao_id']) returnError("请选择账号");
		$result = db('luyou_zu_detail')->insertGetId($data);
        if($result>0){
            $operation_category = 37;
            $detail_name = ['添加路由'];
            $operation_value = ['添加路由组路由'];
            $re_name = ['lyz_ly_add'];
            $re_value = ['1'];
            $arr = array_combine($detail_name, $operation_value);
            $redis = array_combine($re_name, $re_value);
            $this->riZhi($operation_category, $arr, $redis);
            returnSuccess([],"操作成功");
        }else{
            returnError("操作失败");
        }
	}
	
	// 编辑数据
	public function table_xgsj()
	{
	    $data['id'] = input('id/d');
	    $data['nw_bz'] = input('nw_bz/d');
	    $data['zu_id'] = input('zu_id/d');
	    $data['zhanghao_id'] = input('zhanghao_id/d');
	    $data['heyue_zu_bh'] = input('heyue_zu_bh','','trim');
	    if (!$data['zhanghao_id']) returnError("请选择账号");
	    if (!$data['id']) returnError("缺少参数");
		$result = db('luyou_zu_detail')->where('id',$data['id'])->where('zu_id',$data['zu_id'])->update(['heyue_zu_bh'=>$data['heyue_zu_bh'],'nw_bz'=>$data['nw_bz'],'zhanghao_id'=>$data['zhanghao_id'],]);
        if($result>0){
            $operation_category = 38;
            $detail_name = ['编辑路由'];
            $operation_value = ['编辑路由组路由'];
            $re_name = ['lyz_ly_eidt'];
            $re_value = ['1'];
            $arr = array_combine($detail_name, $operation_value);
            $redis = array_combine($re_name, $re_value);
            $this->riZhi($operation_category, $arr, $redis);
            returnSuccess(['zu_id'=>$data['zu_id']],"操作成功");
        }else{
            returnError("操作失败",['zu_id'=>$data['zu_id']]);
        }
	}
	
	// 获取账号类型
	public function zhm()
	{
	    $data = input();
	    if (!isset($data['nwp_id'])) returnError("缺少参数");
		$result = db('trade_service_info')->where('nwp_bz',$data['nwp_id'])->select();
		returnSuccess($result);
	}
}